package com.guigu.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.mapper.MenuRoleMapper;
import com.guigu.mapper.MenusMapper;
import com.guigu.pojo.MenuRole;
import com.guigu.pojo.Menus;
import com.guigu.service.MenuRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.*;

@Service
public class MenuRoleServiceImpl extends ServiceImpl<MenuRoleMapper,MenuRole> implements MenuRoleService {

    @Autowired
    MenuRoleMapper menuRoleMapper;

    @Autowired
    MenusMapper menusMapper;

    //做角色权限添加
    @Override
    public Map authoritySystem(MenuRole menuRole) {

        Map map =new HashMap();
        map.put("msg","授权失败");

        QueryWrapper<MenuRole> queryWrapper
                = new QueryWrapper<>();


        MenuRole menuRole1 = new MenuRole();
        List<Menus> menus2 = new ArrayList<>();
        List<Menus> menus3 = new ArrayList<>();
        List<Menus> menus = new ArrayList<>();
        List<Integer> pid = new ArrayList<>();
        //查询父级id
        for (Object m : menuRole.getListMid()) {
            System.out.println("1菜单id"+m);
            menus =menusMapper.queryallparement(m);
            pid.add(Integer.parseInt(m.toString()));
            for (Menus y : menus) {
                System.out.println("2菜单id"+y);
                menus3 = menusMapper.queryallparement(y.getParentId());
                pid.add(y.getId());
                for (Menus r : menus3) {
                    System.out.println("3菜单id"+r);
                    menus2 =menusMapper.queryallparement(r.getParentId());
                        pid.add(r.getId());
                    for (Menus x : menus2) {
                        System.out.println("4菜单id"+m);
                        pid.add(x.getId());
                    }
                }

            }
        }

        //拿到所有的授权菜单并且去重复
        LinkedHashSet hashSet = new LinkedHashSet(pid);
        ArrayList<Integer> arrayList = new ArrayList<Integer>(hashSet);
        //存到临时集合待会做添加
        ArrayList<Integer> nonull = new ArrayList<Integer>();
        for (Integer integer : arrayList) {
            if (integer!=null){
                nonull.add(integer);
            }
        }
        menuRole1.setMidList(nonull);


            //做添加之前先删除要添加的菜单id不管有没有
            if (menuRoleMapper.deleMenuRole(menuRole.getRId())>0){
                map.put("msg","授权成功");
            }

        for (Object m : menuRole1.getMidList()) {
            System.out.println("所有菜单id"+m);
            //添加
            menuRoleMapper.addMenuRole(m,menuRole                                                                                                                                                                                                                                                                      .getRId());
            map.put("msg","授权成功");
        }

        return map;
    }
}
